package com.soft.javase.day15.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/27
 */
public class LogHandler implements InvocationHandler {
    private CalculatorImpl target;

    public LogHandler(CalculatorImpl target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
        System.out.print("日志: the method " + method.getName() + " begin with[");
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                if (i > 0) {
                    System.out.print(", ");
                }
                System.out.print(args[i]);
            }
        }
        System.out.println("]");

        Object result = method.invoke(target, args);

        System.out.println("日志: the method " + method.getName() + " end with " + result);

        return result;
    }
}
